﻿var XTreePanel = [];

XTreePanel.refreshTree = function (tree, result) {
    var nodes = eval(result);
    if (nodes.length > 0) {
        tree.initChildren(nodes);
    }
    else {
        tree.getRootNode().removeChildren();
    }
}

XTreePanel.clearFilter = function (tree, field) {
    field.setValue("");
    tree.clearFilter();
    tree.getRootNode().collapseChildNodes(true);
    tree.getRootNode().ensureVisible();
};

XTreePanel.filterTree = function (tree, field, e) {
    var text = field.getRawValue();
    tree.clearFilter();
    if (Ext.isEmpty(text, false)) {
        return;
    }

    if (e.getKey() === Ext.EventObject.ESC) {
        //clearFilter();
        XTreePanel.clearFilter(tree, field);
    } else {
        var re = new RegExp(".*" + text + ".*", "i");

        tree.filterBy(function (node) {
            return re.test(node.text);
        });
    }
};
